package org.gfids.trivial.model.servidor;

import org.gfids.trivial.model.Cuestion;
import org.gfids.trivial.model.servidor.BaseDeDatos;
import org.gfids.util.NetSession;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Hilo extends Thread {

    Socket s;
    NetSession nss;
    

    Hilo(Socket s) {
        this.s = s;
        nss = new NetSession(s);
    }
    
    

    @Override
    public void run() {
        super.run();

        try {
        	
        	
        	Object obj = (Object) nss.receive();
        	
            if (obj instanceof Cuestion) {
                soyCuestion(obj);
                nss.send("Lo logramos. :-D");
//                in.close();
            } else if (obj instanceof String) {
                soyString(obj);
//                in.close();
            }
        } catch (Exception ex) {
            Logger.getLogger(Hilo.class.getName()).log(Level.SEVERE, Hilo.class.getName(), ex);
            System.out.println("La razón es : "+ex.getMessage());
        } finally {
        	nss.close();
        }
    }

    public void soyString(Object obj) throws Exception {
        String ss = (String) obj;
        
        nss.send(new StringBuffer(ss).reverse().toString());
    }

    public void soyCuestion(Object obj) throws Exception {
        Cuestion c = (Cuestion) obj;
        BaseDeDatos bd = new BaseDeDatos();
        bd.guardarCuestion(c);
    }
    
}
